package com.danton.user.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.danton.common.bean.CommonResponse;
import com.danton.framework.BaseController;
import com.danton.model.BsPermissionModel;
import com.danton.user.service.PermissionService;
import com.danton.user.service.impl.PermissionServiceImpl;
import com.jfinal.plugin.activerecord.Page;

public class PermissionController extends BaseController {
	protected static final Logger logger = LoggerFactory.getLogger(PermissionController.class);
	private PermissionService permissionService = enhance(PermissionServiceImpl.class);// 支持事务处理

	public void list() {
		int pageNumber = getParaToInt("page_number", page_number);
		int pageSize = getParaToInt("page_size", page_size);
		Page<BsPermissionModel> page = permissionService.list(pageNumber, pageSize);
		CommonResponse respones = CommonResponse.buildResponse();
		respones.getBody().put("page", page);
		renderJson(respones);
	}

	public void add() {
		String resource = getPara("resource");
		String description = getPara("description");
		String remark = getPara("remark");
		String module = getPara("module");
		BsPermissionModel permissionModel = new BsPermissionModel();
		permissionModel.set("resource", resource).set("description", description).set("remark", remark).set("module",
				module);
		boolean flag = permissionService.add(permissionModel);
		if (flag) {
			renderJson(CommonResponse.buildResponse());
			return;
		}
		renderJson(CommonResponse.buildResponse(1, "记录已经存在！"));// errno=1,记录已存在
	}

	public void edit() {
		int permission_id = getParaToInt("permission_id");
		String resource = getPara("resource");
		String description = getPara("description");
		String remark = getPara("remark");
		String module = getPara("module");
		BsPermissionModel permissionModel = new BsPermissionModel();
		permissionModel.set("id", permission_id).set("resource", resource).set("description", description)
				.set("remark", remark).set("module", module);
		boolean flag = permissionService.edit(permissionModel);
		if (flag) {
			renderJson(CommonResponse.buildResponse());
			return;
		}
		renderJson(CommonResponse.buildResponse(1, "记录已经存在！"));// errno=1,记录已存在
	}
}
